package org.example.demopaper.service;

import org.example.demopaper.dao.PaperDao;
import org.example.demopaper.entity.Paper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PaperService implements PaperDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Override
    public Integer insertPaper(Paper paper) {
        String sql = "INSERT INTO paper(title, author, abstracts, pdf_url, pub_date) VALUES (?, ?, ?, ?, ?)";
        return jdbcTemplate.update(sql, paper.getTitle(), paper.getAuthor(), paper.getAbstracts(),
                paper.getPdf_url(), paper.getPub_date());
    }
    @Override
    public Integer editPaper(Paper paper) {
        String sql = "UPDATE paper SET title=?, author=?, abstracts=?, pdf_url=?, pub_date=? WHERE id=?";
        return jdbcTemplate.update(sql, paper.getTitle(), paper.getAuthor(), paper.getAbstracts(),
                paper.getPdf_url(), paper.getPub_date(), paper.getId());
    }
    @Override
    public Integer deletePaper(int id) {
        String sql = "DELETE FROM paper WHERE id = ?";
        return jdbcTemplate.update(sql, id);
    }
    @Override
    public Paper getPaperById(int id) {
        String sql = "SELECT * FROM paper WHERE id = ?";
        return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(Paper.class));
    }
    @Override
    public List<Paper> getAllPapers() {
        String sql = "SELECT * FROM paper";
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Paper.class));
    }
}